package com.appkubes.fintech.admin.application.order.service.impl;


import cn.hutool.core.date.DateUtil;
import com.appkubes.fintech.admin.application.order.entity.dto.RentRepayOrderDTO;
import com.appkubes.fintech.admin.application.order.dao.RentRepayOrderDao;
import com.appkubes.fintech.admin.application.order.service.RentRepayOrderService;
import com.appkubes.fintech.admin.application.order.entity.vo.RentRepayOrderVO;
import com.appkubes.fintech.common.util.ConversionUtil;
import com.appkubes.fintech.core.po.FintechRentRepayOrder;
import com.appkubes.fintech.core.po.FintechRentRepayOrderExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.appkubes.fintech.common.response.Response;

import javax.annotation.Resource;

/*
 * 还款单
 * @author    xqc
 * @date    2020/01/04 23:48:56
 */
@Service
@Slf4j
public class RentRepayOrderServiceImpl implements RentRepayOrderService {


    @Resource
    private RentRepayOrderDao rentRepayOrderDao;


    @Override
    public PageInfo<RentRepayOrderDTO> queryPage(RentRepayOrderVO rentRepayOrderVO) {
        PageHelper.startPage(rentRepayOrderVO.getPage(), rentRepayOrderVO.getPageSize());
        List<RentRepayOrderDTO> responseList = rentRepayOrderDao.queryRePayOrder(rentRepayOrderVO);
        return new PageInfo<>(responseList);
    }


    @Override
    public Response save(RentRepayOrderVO rentRepayOrderVO) {
        FintechRentRepayOrder rentRepayOrder = new FintechRentRepayOrder();
        BeanUtils.copyProperties(rentRepayOrderVO, rentRepayOrder);
        rentRepayOrderDao.insert(rentRepayOrder);
        return Response.success();
    }


    @Override
    public Response update(RentRepayOrderVO rentRepayOrderVO) {
        if (StringUtils.isBlank(rentRepayOrderVO.getRepayKey())) {
            return Response.fail("主键不能为空");
        }
        FintechRentRepayOrder old = rentRepayOrderDao.selectByPrimaryKey(rentRepayOrderVO.getRepayKey());
        if (old == null) {
            return Response.fail("对象不存在");
        }
        BeanUtils.copyProperties(rentRepayOrderVO, old, "id", "createTime", "updateTime");
        rentRepayOrderDao.updateByPrimaryKey(old);
        return Response.success();
    }


    @Override
    public Response<RentRepayOrderDTO> getById(String id) {
        FintechRentRepayOrder rentRepayOrder = rentRepayOrderDao.selectByPrimaryKey(id);
        RentRepayOrderDTO rentRepayOrderDTO = new RentRepayOrderDTO();
        BeanUtils.copyProperties(rentRepayOrder, rentRepayOrderDTO);
        return Response.success(rentRepayOrderDTO);

    }

    @Override
    public void deleteById(String id) {
        rentRepayOrderDao.deleteByPrimaryKey(id);
    }


}
